<script setup>
import { reactive } from '@vue/reactivity';
import { onMounted } from '@vue/runtime-core';
import { post } from '../../utils'

//#region 列表逻辑
let listData = reactive({
    condition: {
        sys: import.meta.env.VITE_SYS_NAME,
        pageIndex: 1,
        pageSize: 10,
    },
    loading: false,
    list: [],
    totalCount: 0,
});
function getList() {
    listData.loading = true;
    post('/api/admin/GetSysRoleList', listData.condition)
        .then(function (res) {
            listData.loading = false;
            listData.list = res.result.list;
            listData.totalCount = res.result.totalCount;
        })
}
function searchList() {
    listData.condition.pageIndex = 1;
    getList();
}
function pageChange(page) {
    listData.condition.pageIndex = page;
    getList();
}
onMounted(() => {
    getList();
})
//#endregion

//#region 编辑逻辑
import EditDialog from './EditDialog.vue';
let editDialog;
//#endregion

//#region 删除逻辑
function doDelete(row) {
    post('/api/admin/DeleteSysRole', { id: row.id }).then(res => {
        if (res.success) {
            getList();
        }
    });
}
//#endregion

//设置权限
import SetRoleResDialog from './SetRoleResDialog.vue';
let setRoleResDialog;
</script>

<template>
    <div class="mb-2">
        <el-button type="primary" class="el-icon-plus" @click="editDialog.showEdit(0)">添加</el-button>
    </div>

    <el-table v-loading="listData.loading" :data="listData.list" :border="true" style="width: 100%">
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="addTime" label="添加时间"></el-table-column>

        <el-table-column label="操作" width="150">
            <template #default="scope">
                <el-button @click="setRoleResDialog.show(scope.row.id)" type="text">设置权限</el-button>
                <el-button @click="editDialog.showEdit(scope.row.id)" type="text">编辑</el-button>
                <el-popconfirm title="确定要删除这条数据吗？" @confirm="doDelete(scope.row)">
                    <template #reference>
                        <el-button type="text">删除</el-button>
                    </template>
                </el-popconfirm>
            </template>
        </el-table-column>
    </el-table>
    <div class="mt-2">
        <el-pagination
            background
            layout="prev, pager, next"
            @current-change="pageChange"
            hide-on-single-page
            :page-size="10"
            :total="listData.totalCount"
        ></el-pagination>
    </div>
    <EditDialog ref="editDialog" @edit="getList" />
    <SetRoleResDialog ref="setRoleResDialog"></SetRoleResDialog>
</template>